<?php
/**
  * Implement command WNavigator/getPage
 * <pre>
 * Arguments:
 * 1. path (string, mandatory)
 *    path to page or a set of paths separated with ';', 
 * 	  also accepted path with '/*n' at end, it means get pages subtree with depth 'n' 
 * 2. extend (boolean, optional) 
 *    Return full pages subtree or not. Default value "false".
 * Result:
 * Data structure   
 * page
 * |-name
 * |-meta [uri]
 * | |-<value>+
 * |-content
 * |-children
 * | |-page+
 * </pre>
 *  
 * @package WNavigator
 * 
 */
//TODO: setup license
require_once 'WKernel/Command/Abstract.php';
class WN_Cmd_getPages extends WK_Command_Abstract {
	
	public function exec(){
		$nav  = WK_Application::getComponent('WNavigator');
		$paths = explode(';', $this->params['path']);
		$this->result = new WK_Data();
		foreach($paths as $i => $path) {
			$expand = 0;
			$matches = array();
			
			if(preg_match('/.*\/\*([0-9]+)?$/sU', $path, $matches)){
				if(!empty($matches[1]))
					$expand = $matches[1];
				else
					$expand = -1;
				//FIXME: don't works with path '/*'
				$path   = str_replace('/*' . @$matches[1], '', $path);
			}
			$page = $nav->getMap()->getPage($path);
			$collection = new WK_Data_Collection();
			if(!empty($page)) {
				$data = $page->toData($expand);
				if($expand != 0) {
					$data = $data->getValue('children');		
					$collection->merge($data);
				}
				else {
					$collection->push($data->toStructure());
				}
			}
		}
		$this->result->newValue('pages', $collection);	
	}	
}